package com.draemoncode.weixindb.action;

import com.draemoncode.weixindb.constant.MessagePackageTypeConstant;
import com.draemoncode.weixindb.constant.PodFlagConstant;
import com.draemoncode.weixindb.constant.ShangZhuangFieldConstant;
import com.draemoncode.weixindb.domain.B_Pod;
import com.draemoncode.weixindb.mvc.exception.BusiException;
import com.draemoncode.weixindb.repository.B_PodRepository;
import com.draemoncode.weixindb.server.CMessagePackage;
import com.draemoncode.weixindb.server.IAction;
import com.draemoncode.weixindb.service.BusiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 *
 * @author yangz
 * @date 2017/4/9
 *
 */
@Component
public class StartPodAction implements IAction<B_Pod, Object>{

    public final static String ACTION = "start";

    @Autowired
    private B_PodRepository b_podRepository;

    @Autowired
    private BusiService busiService;

    @Override
    public Boolean shoudProcess(String action) {
        return ACTION.equals(action);
    }

    @Override
    public B_Pod busiExec() {
        B_Pod pod = b_podRepository.findByFlag(PodFlagConstant.ING);
        if(pod == null){
            throw new BusiException(HttpStatus.BAD_REQUEST.value(), "没人上庄");
        }
        return pod;
    }

    @Override
    public Object uiExec(B_Pod busiResult) {
        return null;
    }

    @Override
    public CMessagePackage buildResponse(B_Pod busiResult, Object uiResult) {
        CMessagePackage response = new CMessagePackage(MessagePackageTypeConstant.RESPONSE_START);
        response.setSyncid(busiResult.getId());
        String zhuangName = busiService.buildCurrZhuangJiaMessage(busiResult.getZhuang_name(), busiResult.getZhuang_money());
        response.getMsg().put(ShangZhuangFieldConstant.RESPONSE_SHOW_CONTENT, zhuangName);
        return response;
    }
}
